<?php
/*
	Class to do operations with database
*/
include_once("db_connection.php");
class dbm_DataBaseManager
{
	var $iResultID = 0;
	
	//select function
	function selectSQL($sSql="")
	{
		//get vars
		$db_vars = new dbm_vars();
		$errPath = $db_vars->getErrorLogPath();
		
		//do connection 
		$db_connect = new dbm_Connection();
		$connect = $db_connect->dbConnection();
		if($connect!=0)
		{
			//if the connection is successful, execute query
			$this->iResultID = mysql_query($sSql,$connect);
			if(!$this->iResultID)
			{
				return 0;		//if the query return empty 
				$fp = fopen($errPath,"w");
				fwrite($fp,mysql_errno($connect)." ".mysql_error($connect));
				fclose($fp);
			}
			else
			{	
				//if the query returns values, this are converted to json
				$numRows = mysql_num_rows($this->iResultID);
				for($i = 0; $i < $numRows; $i++)
				{
					$resultArray[$i] = mysql_fetch_array($this->iResultID);
				}
				return json_encode($resultArray);
			}
		}
		else
		{
			return 0;		//connection failed
			$fp = fopen($errPath,"w");
			fwrite($fp,mysql_errno($connect)." ".mysql_error($connect));
			fclose($fp);
		}
	}
	
	//function to do INSERT,UPDATE,DELETE.
	function querySQL($sSql="")
	{
		//get vars
		$db_vars = new dbm_vars();
		$errPath = $db_vars->getErrorLogPath();
		
		//do connection 
		$db_connect = new dbm_Connection();
		$connect = $db_connect->dbConnection();
		if($connect!=0)
		{
			//if the connection is successful, execute query
			$this->iResultID = mysql_query($sSql,$connect);
			if(!$this->iResultID)
			{
				return 0;		//if the query return empty 
				$fp = fopen($errPath,"w");
				fwrite($fp,mysql_errno($connect)." ".mysql_error($connect));
				fclose();										 
			}
			else
			{
				//returns the number of rows affected by the query
				return json_encode(mysql_affected_rows());	
			}
		}
		else
		{
			return 0;		//connection failed
			$fp = fopen($errPath,"w");
			fwrite($fp,mysql_errno($connect)." ".mysql_error($connect));
			fclose();	
		}
	}
}
?>